<?php

namespace App\Services;

use App\Constants\CacheKey;
use App\Constants\ErrorCode;
use App\Services\Dao\ChatDao;
use App\Services\Formatter\ChatFormatter;
use App\Services\Formatter\MeetingFormatter;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Redis\Redis;
use TheSeer\Tokenizer\Exception;

class WsService
{
    /**
     * @Inject
     * @var ChatFormatter
     */
    protected $chatFormatter;

    /**
     * @Inject
     * @var ChatDao
     */
    protected $chatDao;

    /**
     * @Inject
     * @var MeetingService
     */
    protected $meetingService;

    /**
     * @Inject
     * @var MeetingFormatter
     */
    protected $meetingFormatter;

    /**
     * 无token访问白名单
     * @var array
     */
    private array $whiteList = ['getChatMsg', 'getMeetings'];

    public function calls($data, string $token = ''): array
    {
        $method = $data['method'] ?? "";
        if (!method_exists($this, $method)) {
            return return_response_data(ErrorCode::CALL_METHOD_NOT_EXIST, ErrorCode::getMessage(ErrorCode::CALL_METHOD_NOT_EXIST));
        }
        $redis = di()->get(Redis::class);
        if ($redis->exists(CacheKey::USER_AUTH_KEY . $token)) {
            return call_user_func([$this, $method], $data);
        } else {
            if (in_array($method, $this->whiteList)) {
                return call_user_func([$this, $method], $data);
            }
          return return_response_data(ErrorCode::TOKEN_INVALID, ErrorCode::getMessage(ErrorCode::TOKEN_INVALID));
        }
    }

    /**
     * note:聊天列表数据
     * @param array $data
     * created by: lhw at 13/1/2022 10:15 下午
     * @return array
     */
    public function getChatMsg(array $data): array
    {
        if (empty($data['meetingId']) && empty($data['relatedId'])) {
            return  return_response_data(ErrorCode::PARAMS_ERROR, ErrorCode::getMessage(ErrorCode::PARAMS_ERROR));
        }
        $meetingUser = 0;
        if (!empty($data['meetingId'])) {
            $meeting = $this->meetingService->findInfoById($data['meetingId']);
            $meetingUser = $meeting['userId'] ?? 0;
        }
        $result = $this->chatDao->getMsgList($data);
        $list = [
            "total" => $result->total(),
            "currentPage" => $result->currentPage(),
            "hasMore" => $result->hasMorePages(),
            "items" =>  $this->chatFormatter->formatList($result, $meetingUser)
        ];
        return  return_response_data(0, 'success', $list);
    }

    /**
     * note:检查活动是否存在
     * @param int $meetingId
     * created by: lhw at 14/1/2022 9:17 下午
     * @return bool
     */
    public function checkMeeting(int $meetingId): bool
    {
        $meeting = $this->meetingService->findInfoById($meetingId);
        if (empty($meeting)) {
            return false;
        }
        if ($meeting['status'] == 2) {
            return false;
        }
        return true;
    }

    /**
     * note: 获取活动列表
     * @param array $data
     * created by: lhw at 13/2/2022 3:47 下午
     * @return array
     */
    public function getMeetings(array $data): array
    {
        $all = $this->meetingService->allNoFinishMeetings();
        if (empty($all->all())) {
            return return_response_data(0, 'success', []);
        }
        $result = $this->meetingFormatter->formatList($all);
        return return_response_data(0, 'success', $result);
    }
}
